package org.southyin.springboot02.controller;

import org.southyin.springboot02.commons.CommonsPage;
import org.southyin.springboot02.commons.CommonsResp;
import org.southyin.springboot02.model.CmsArticle;
import org.southyin.springboot02.model.CmsCustomer;
import org.southyin.springboot02.service.CmsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/article")
@CrossOrigin(value = "http://127.0.0.1:5500",allowCredentials = "true")
public class ArticleController {
    @Autowired
    CmsArticleService service;
    @GetMapping("/details")
    public CmsArticle details(Long id) {
        return service.getArticleById(id);
    }

    @PostMapping("/save")
    public CommonsResp save(CmsArticle article, HttpSession session) {
        CmsCustomer loginer = ((CmsCustomer) session.getAttribute("loginer"));
        Long uid = loginer.getId();
        article.setUid(uid);
        return service.saveArticleInfo(article);
    }

    @GetMapping("/index")
    public CommonsPage<CmsArticle> index(@RequestParam(value = "listId",defaultValue = "1") String listId, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "3") int pageSize) {
        return service.listHotOrNewOrHotList(listId, pageNum, pageSize);
    }
}
